﻿<!DOCTYPE HTML>
<html lang="zh">
<head>
<title>Loop - 语法 &amp; 使用 | AutoHotkey v2</title>
<meta name="description" content="The Loop statement performs a series of code lines repeatedly: either the specified number of times or until a Break statement is encountered." />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link href="../static/theme.css" rel="stylesheet" type="text/css" />
<script src="../static/content.js" type="text/javascript"></script>
<script type="text/javascript">$(function(){0<=window.navigator.userAgent.toLowerCase().indexOf("ucbrowser")&&CaoNiMaDeUc()})</script>
</head>
<body>

<h1>Loop (normal)</h1>

<p>Performs a series of functions repeatedly: either the specified number of times or until <a href="Break.htm">break</a> is encountered.</p>

<pre class="Syntax"><span class="func">Loop</span> <span class="optional">Count</span></pre>
<h2 id="Parameters">参数</h2>
<dl>

  <dt>Count</dt>
  <dd>
    <p>类型: <a href="../Concepts.htm#numbers">整数</a></p>
    <p>How many times (iterations) to perform the loop. If omitted, the Loop continues indefinitely until a <a href="Break.htm">break</a> or <a href="Return.htm">return</a> is encountered. However, an explicit blank value or number less than 1 causes the loop to be skipped entirely.</p>
    <p><em>Count</em> is evaluated only once, right before the loop begins. For instance, if <em>Count</em> is an expression with side-effects such as function calls or assignments, the side-effects occur only once.</p>
    <p>If <em>Count</em> is enclosed in parentheses, a space or tab is not required. 例如: <code>Loop(2)</code></p>
  </dd>

</dl>

<h2 id="Remarks">备注</h2>
<p>The loop statement is usually followed by a <a href="Block.htm">block</a>, which is a collection of statements that form the <em>body</em> of the loop. However, a loop with only a single statement does not require a block (an &quot;if&quot; and its &quot;else&quot; count as a single statement for this purpose).</p>
<p>A common use of this statement is an infinite loop that uses the <a href="Break.htm">break</a> statement somewhere in the loop's <em>body</em> to determine when to stop the loop.</p>
<p>The use of <a href="Break.htm">break</a> and <a href="Continue.htm">continue</a> inside a loop are encouraged as alternatives to <a href="Goto.htm">goto</a>, since they generally make a script more understandable and maintainable. One can also create a "While" or "Do...While/Until" loop by making the first or last statement of the loop's <em>body</em> an IF statement that conditionally issues the <a href="Break.htm">break</a> statement, but the use of <a href="While.htm">While</a> or <a href="Until.htm">Loop...Until</a> is usually preferred.</p>
<p>The built-in variable <strong>A_Index</strong> contains the number of the current loop iteration. It contains 1 the first time the loop's <em>body</em> is executed. For the second time, it contains 2; and so on. If an inner loop is enclosed by an outer loop, the inner loop takes precedence. A_Index works inside all types of loops, including <a href="LoopFiles.htm">file-loops</a> and <a href="LoopReg.htm">registry-loops</a>; but A_Index contains 0 outside of a loop.</p>
<p>A_Index can be assigned any integer value by the script. If <em>Count</em> is specified, changing A_Index affects the number of iterations that will be performed. 例如, <code>A_Index := 3</code> would make the loop statement act as though it is on the third iteration (A_Index will be 4 on the next iteration), while <code>A_Index--</code> would prevent the current iteration from being counted toward the total.</p>
<p>The <a href="Block.htm#otb">One True Brace (OTB) style</a> may optionally be used. 例如:</p>
<pre>Loop {
    ...
}
Loop RepeatCount {
    ...
}</pre>
<p>Specialized loops: Loops can be used to automatically retrieve files, folders, or registry items (one at a time). See <a href="LoopFiles.htm">file-loop</a> and <a href="LoopReg.htm">registry-loop</a> for details. In addition, <a href="LoopRead.htm">file-reading loops</a> can operate on the entire contents of a file, one line at a time. Finally, <a href="LoopParse.htm">parsing loops</a> can operate on the individual fields contained inside a delimited string.</p>
<h2 id="Related">相关</h2>
<p><a href="Until.htm">Until</a>, <a href="While.htm">While-loop</a>, <a href="For.htm">For-loop</a>, <a href="LoopFiles.htm">Files-and-folders loop</a>, <a href="LoopReg.htm">Registry loop</a>, <a href="LoopRead.htm">File-reading loop</a>, <a href="LoopParse.htm">Parsing loop</a>, <a href="Break.htm">Break</a>, <a href="Continue.htm">Continue</a>, <a href="Block.htm">Blocks</a></p>
<h2 id="Examples">示例</h2>
<div class="ex" id="ExBasic">
<p><a href="#ExBasic">#1</a></p>
<pre>Loop 3
{
    MsgBox "Iteration number is " A_Index  <em>; A_Index will be 1, 2, then 3</em>
    Sleep 100
}</pre>
</div>

<div class="ex" id="ExBreakContinue">
<p><a href="#ExBreakContinue">#2</a></p>
<pre>Loop
{
    if (A_Index &gt; 25)
        break  <em>; Terminate the loop</em>
    if (A_Index &lt; 20)
        continue <em>; Skip the below and start a new iteration</em>
    MsgBox "A_Index = " A_Index <em>; This will display only the numbers 20 through 25</em>
}</pre>
</div>

</body>
</html>